import calculateFn from '@/pluginPackage/order/confirmOrder/hooks/calculate'
import type { Process } from '@pluginPackage/hooks/registerDisPlugin'
import type { StorageSku } from '@/pluginPackage/goods/commodityInfo/types'
import { Ref } from 'vue'

interface DiscountRulse {
  'addon-coupon': {
    open: boolean
    rules: any
  }
  'addon-full-reduction': {
    open: boolean
    rules: any
  }
  'addon-member': {
    open: boolean
    rules: any
  }
}
const { addDiscount, activeResume, delDiscount } = calculateFn()
export function useCommodityInfoPrice(chooseSku: Ref<StorageSku>, discountRulse: DiscountRulse) {
  for (const key in discountRulse) {
    const rules = discountRulse[key as keyof DiscountRulse].rules
    key === 'addon-member' && discountRulse[key] && setDiscount({ name: key, rule: rules })
    if (discountRulse[key as keyof DiscountRulse]) {
      if (rules) {
        setDiscount({ name: key as keyof DiscountRulse, rule: rules, chooseSku: chooseSku.value })
      } else {
        delDiscount(chooseSku.value.shopId, key as keyof DiscountRulse)
      }
    }
  }
  function setDiscount(params: { name: Process['name']; rule: Process['rule']; chooseSku?: StorageSku }) {
    const { shopId, productId, salePrice } = chooseSku.value
    const process: Process = {
      name: params.name,
      priority: 1,
      rule: params.rule,
      productInfoArr: [
        {
          shopId,
          productId,
          productPrice: salePrice,
          quantity: 1,
        },
      ],
    }

    // 添加优惠项
    addDiscount(shopId, process)
  }
  return activeResume(chooseSku.value.shopId)
}
